import { Request, RequestHandler, Response } from "express";
import {
  IGetMachineAppointmentByIdReq,
  IAddMachineAppointmentReq,
  IUpdateMachineAppointmentReq,
  IDeleteMachineAppointmentReq,
} from "../../models/machine-appointment.model";
import * as MachineAppointmentService from "../services/machine-appointment.service";
import { convertToLocalDateString } from "../utils/date";

/**
 * Get active machine appointment records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getMachineAppointments: RequestHandler = async (
  req: Request,
  res: Response
) => {
  try {
    const machineAppointments =
      await MachineAppointmentService.getMachineAppointments();

    res.status(200).json({
      data: machineAppointments,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][getMachineAppointments][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "仪器预约列表获取失败！",
    });
  }
};

/**
 * Get active machine appointment records
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getAppliedMachineAppointmentsById:  RequestHandler = async (
  req: IGetMachineAppointmentByIdReq,
  res: Response
) => {
  try {
    const machineAppointments =
      await MachineAppointmentService.getAppliedMachineAppointmentsById(req.params.id);

    res.status(200).json({
      data: machineAppointments,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][getAppliedMachineAppointments][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "仪器预约列表获取失败！",
    });
  }
};

/**
 * Get machine appointment record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getMachineAppointmentById: RequestHandler = async (
  req: IGetMachineAppointmentByIdReq,
  res: Response
) => {
  try {
    const machineAppointment =
      await MachineAppointmentService.getMachineAppointmentById(req.params.id);

    res.status(200).json({
      data: machineAppointment,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][getMachineAppointmentById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "仪器预约信息获取失败！",
    });
  }
};

/**
 * Inserts a new machine appointment record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addMachineAppointment: RequestHandler = async (
  req: IAddMachineAppointmentReq,
  res: Response
) => {
  try {
    req.body.createdTime = new Date(Date.now());
    req.body.updatedTime = req.body.createdTime;
    req.body.machineStartDate = req.body.machineStartDate
    ? convertToLocalDateString(req.body.machineStartDate)
    : null;
    req.body.machineEndDate = req.body.machineEndDate
    ? convertToLocalDateString(req.body.machineEndDate)
    : null;

    const result = await MachineAppointmentService.insertMachineAppointment(
      req.body
    );

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][addMachineAppointment][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    if (error.code === "ER_DUP_ENTRY") {
      res.status(500).json({
        message: "仪器预约名称重复，请重新输入！",
      });
    } else {
      res.status(500).json({
        message: "仪器预约保存失败！",
      });
    }
  }
};

/**
 * Updates existing machine appointment record
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const updateMachineAppointmentById: RequestHandler = async (
  req: IUpdateMachineAppointmentReq,
  res: Response
) => {
  try {
    req.body.updatedTime = new Date(Date.now());
    req.body.machineStartDate = req.body.machineStartDate
      ? req.body.machineStartDate.length === 10 ?req.body.machineStartDate : convertToLocalDateString(req.body.machineStartDate)
      : null;
    req.body.machineEndDate = req.body.machineEndDate
      ? req.body.machineEndDate.length === 10 ?req.body.machineEndDate : convertToLocalDateString(req.body.machineEndDate)
      : null;

    const result = await MachineAppointmentService.updateMachineAppointment({
      ...req.body,
      id: req.params.id,
    });

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][updateMachineAppointmentById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    if (error.code === "ER_DUP_ENTRY") {
      res.status(500).json({
        message: "仪器预约名称重复，请重新输入！",
      });
    } else {
      res.status(500).json({
        message: "仪器预约更新失败！",
      });
    }
  }
};

/**
 * deletes a machine appointment
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteMachineAppointmentById: RequestHandler = async (
  req: IDeleteMachineAppointmentReq,
  res: Response
) => {
  try {
    const result = await MachineAppointmentService.deleteMachineAppointment(
      req.params.id
    );

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error(
      "[machineAppointment.controller][deleteMachineAppointmentById][Error] ",
      typeof error === "object" ? JSON.stringify(error) : error
    );
    res.status(500).json({
      message: "仪器预约删除失败！",
    });
  }
};
